package com.richman.ps38;

import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.richman.controller.DaShui;
import com.richman.ps38.entity.Event;

public class PS38Manager {

	
	public static Vector<Event> ps38Events = new Vector<Event>();
	
	public static String ps38UpdateStr = "";
	public static Vector<Event> ps38DanshiEvents = new Vector<Event>();
	public static  ReadWriteLock danshiEventsLock = new ReentrantReadWriteLock();
	
	public Vector<PS38Selenium> accounts = new Vector<PS38Selenium>();
	
	public Vector<PS38Selenium> danshiAccounts = new Vector<PS38Selenium>();
	
	public void init() {
		
		Vector<String[]> tmps = DaShui.mainWnd.getPS38Accounts();
		for(int i = 0; i <tmps.size(); i++ ) {
			
			PS38Selenium ps38Selenium = new PS38Selenium();
			ps38Selenium.setLoginParams(tmps.elementAt(i)[0], tmps.elementAt(i)[1],
					tmps.elementAt(i)[2]);

			accounts.add(ps38Selenium);
			
		}
		
		Vector<String[]> tmps1 = DaShui.mainWnd.getPS38DanshiAccounts();
		for(int i = 0; i <tmps1.size(); i++ ) {
			
			PS38Selenium ps38Selenium = new PS38Selenium();
			ps38Selenium.setLoginParams(tmps1.elementAt(i)[0], tmps1.elementAt(i)[1],
					tmps1.elementAt(i)[2]);

			danshiAccounts.add(ps38Selenium);
			
		}
		
	}
	
	
	

	
	
	public void login() {
		
		
		try {
			
			for(int i = 0; i <accounts.size(); i++ ) {				
				if(accounts.elementAt(i).getStatusCode() == -1) {
					LoginThread lt = new LoginThread();
					lt.setPS38Selenium(accounts.elementAt(i));
					lt.start();					
					
					
					
					Thread.sleep(2000);
					
					System.out.println(accounts.elementAt(i).getStatusCode());
					
				}
			}			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
	}
	

	public void loginDanshi() {
		
		
		try {
			
			for(int i = 0; i <danshiAccounts.size(); i++ ) {				
				if(danshiAccounts.elementAt(i).getStatusCode() == -1) {
					LoginThread lt = new LoginThread();
					lt.setPS38Selenium(danshiAccounts.elementAt(i));
					lt.start();					
					Thread.sleep(2000);
					
					System.out.println(danshiAccounts.elementAt(i).getStatusCode());
					
				}
			}			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
	}

	

	
	public void grabEvents() {
		
		
		try {
			
			
			
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
			
			String showText = "";
			
			String successText = "";
			String failText = "";
			String notUsefulText = "";
			
			
			int second = DaShui.mainWnd.getUpdateSeconds();
			
			int usefulAccountNum = 0;
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				if(accounts.elementAt(i).getStatusCode() != -1 && accounts.elementAt(i).getStatusCode() != 0) {
					usefulAccountNum++;
				}
			}
			
			
			
			if(usefulAccountNum*second < 4000 && usefulAccountNum != 0) {
				second = 4000/usefulAccountNum;
			}
			
			int millis = second;
			
			
			String failstr = " 登录中";
			if(System.currentTimeMillis() - DaShui.startEngineTime > 150*1000) {
				failstr = " 无法获取赛事列表";
			}
			
			
			for(int i = 0; i <accounts.size(); i++ ) {
				
				int statusCode = accounts.elementAt(i).getStatusCode();
				
				if(statusCode != -1 && statusCode != 0) {
					accounts.elementAt(i).getMatches();
				}
				
				
				if(accounts.elementAt(i).getStatusCode() == 3) {
					successText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}else {
					failText += "  " +accounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}
				
				if(!successText.equals("")) {
					showText =  successText + " 获取赛事列表成功" + "\n";
					
				
					
				}
				
				if(!failText.equals("")) {
					showText += failText + failstr + "\n";
				}
				
				ps38UpdateStr =showText;
				
				DaShui.mainWnd.updatePS38TextArea(showText);

				DaShui.mainWnd.updatePS38AccountStatus(accounts.elementAt(i).getAccount(), accounts.elementAt(i).getStatusCode());
				
				Thread.sleep(3000);

			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
	}
	
	
	public void getBalance() {
		for(int i = 0; i <accounts.size(); i++ ) {
			
			int statusCode = accounts.elementAt(i).getStatusCode();
			
			if(statusCode != -1 && statusCode != 0) {
				accounts.elementAt(i).getBalance();
			}
		}
	}
	
	
	public void grabDanshiEvents() {
		
		
		try {
			
			
			
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
			
			String showText = "";
			
			String successText = "";
			String failText = "";
			String notUsefulText = "";
			
			
			int second = DaShui.mainWnd.getUpdateSeconds();
			
			int usefulAccountNum = 0;
			
			for(int i = 0; i <danshiAccounts.size(); i++ ) {
				
				if(danshiAccounts.elementAt(i).getStatusCode() != -1 && danshiAccounts.elementAt(i).getStatusCode() != 0) {
					usefulAccountNum++;
				}
			}
			
			
			
			if(usefulAccountNum*second < 4000 && usefulAccountNum != 0) {
				second = 4000/usefulAccountNum;
			}
			
			int millis = second;
			
			
			String failstr = " 登录中";
			if(System.currentTimeMillis() - DaShui.startEngineTime > 150*1000) {
				failstr = " 无法获取赛事列表";
			}
			
			
			for(int i = 0; i <danshiAccounts.size(); i++ ) {
				
				int statusCode = danshiAccounts.elementAt(i).getStatusCode();
				
				if(statusCode != -1 && statusCode != 0) {
					danshiAccounts.elementAt(i).getDanshiMatches();
				}
				
				
				if(danshiAccounts.elementAt(i).getStatusCode() == 3) {
					successText += "  " +danshiAccounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}else {
					failText += "  " +danshiAccounts.elementAt(i).getAccount() + "  " + df.format(System.currentTimeMillis()) + " "; 
				}
				
				if(!successText.equals("")) {
					showText =  successText + " 获取赛事列表成功" + "\n";
				}
				
				if(!failText.equals("")) {
					showText += failText + failstr + "\n";
				}
				
				
				
				DaShui.mainWnd.updatePS38TextArea(showText);

				DaShui.mainWnd.updatePS38AccountStatus(danshiAccounts.elementAt(i).getAccount(), danshiAccounts.elementAt(i).getStatusCode());
				
				Thread.sleep(millis);

			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		
	}
	
}
